import io

from PIL import Image
import QR

from PIL import ImageDraw


# im = Image.open("D:\PyProject\pythonProject\IMG_0167.HEIC.JPG")
# print(im.format,im.size,im.mode)
# im.show()

# size = (128*128)
# for infile in sys.argv[1:]:
#     outfile = os.path.splitext(infile)[0] + ".thumbnail"
#     if infile != outfile:
#         try:
#             with Image.open(infile) as im:
#                 im.thumbnail(size)
#                 im.save(outfile, "JPEG")
#         except OSError:
#             print("cannot create thumbnail for", infile)

# 打开背景图片
def send_photo(user_name, user_email, request):
    QRid, buffered = QR.ORdata(user_name, user_email, request)

    background = Image.open('static/images/hellocoffee.png')
    # 打开二维码图片
    qrcode_image = Image.open(buffered)
    # 设置二维码图片大小
    foreground = qrcode_image.resize((300, 300))
    # 将二维码图片合并至背景图
    background.paste(foreground, (200, 400))
    # 写入文字
    draw = ImageDraw.Draw(background)
    draw.text((900, 650), f"NO.{QRid:0>10}", (73, 98, 171), font_size=50)
    # 保存图片
    buffered = io.BytesIO()
    background.save(buffered, format="PNG")
    buffered.seek(0)
    return buffered
